package com.bw.mallshop.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bw.mallshop.R;
import com.bw.mallshop.entity.HomeBannerEntity;

import java.util.List;

public class HomeVpAdapter extends PagerAdapter {

    private List<HomeBannerEntity> list;

    public HomeVpAdapter(List<HomeBannerEntity> list){
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        View view = (View) object;
        container.removeView(view);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext()).
                inflate(R.layout.itme_home_top,container,false);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(lp);
        container.addView(view);
        ImageView imageView = view.findViewById(R.id.home_top_img);
        Glide.with(container.getContext()).
                load(list.get(position).imgUrl)
                .centerCrop()
                .into(imageView);
        return view;
    }
}
